package com.library.search.dao;

import com.framework.BaseDao;
import com.library.entity.StaffSearch;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Repository
public class SearchDaoImpl extends BaseDao<StaffSearch,String> {
	@Resource
	private SessionFactory sessionFactory;

	public StaffSearch getSearchById(Integer searchId){
		try{
			return super.get(searchId);
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	//精确查找
	public StaffSearch getSearchByKey(String key){
		try{
			return super.findOne("from StaffSearch s where s.searchKey like ?",new Object[]{key});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	/*查找条件搜索数量*/
	public Long findCountByProperty(Map paramsMap){
		int i = 0;
		int j = 0;
		String key;
		StringBuilder hql = new StringBuilder("from StaffSearch s");
		List<Object> params = new ArrayList();

		if(paramsMap.size() != 0){
			Iterator<String> it = paramsMap.keySet().iterator();
			hql.append(" where ");
			while (it.hasNext()) {
				key = it.next();
				params.add(paramsMap.get(key));

				if(j != 0){
					if(params.get(i) != null)
						hql.append(" and s."+ key + "=?");
					else{
						hql.append(" and s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}
				} else{
					if(params.get(i) != null)
						hql.append(" s."+ key + "=?");
					else{
						hql.append(" s."+ key + " is null");
						params.remove(i);
						++j;
						continue;
					}

				}
				++i;
				++j;
			}
		}
		try{
			return this.getStaffSearchsCount(hql.toString(),params.toArray());
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}

	/*按条件查询数量*/
	public Long getStaffSearchsCount(String hql,Object[] params){
		try{
			return super.findCountByPage("select sum(searchCount) " + hql,params);
		}catch(Exception e){
			e.printStackTrace();
			return (long)0;
		}
	}


	public void saveSearch(StaffSearch search){
		try {
			this.save(search);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updateSearch(StaffSearch search){
		try {
			this.update(search);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public List<StaffSearch> getAllOrderBySearchCount(){
		try{
			return super.findByProperty("from StaffSearch s order by s.searchCount desc", new Object[]{});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public List<StaffSearch> getHotOrderBySearchCount(int num){
		String hql = "from StaffSearch s order by s.searchCount desc";
		Object[] params = new Object[]{};
		try{
			Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setMaxResults(num);			//设置最大查询数量
			if (params != null && params.length > 0) {
				for (int i = 0; i < params.length; i++) {
					query.setParameter(i, params[i]);
				}
			}
			return query.list();
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public void deleteSearch(StaffSearch search){
		try {
			this.delete(search);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//模糊查找
	public List<StaffSearch> getSearchByKeyVague(String key){
		try{
				key = new StringBuffer(key).append("%").toString();
			return super.findByProperty("from StaffSearch s where s.searchKey like ? order by s.searchCount desc", new Object[]{key});
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
}
